<!doctype html>
<html>

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
  <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
  <title>微信登录绑定手机号码</title>
  <link rel="stylesheet" type="text/css" href="../css/api.css" />
  <link rel="stylesheet" type="text/css" href="../css/register.css" />
</head>

<body>
  <div id="app" v-cloak>
    <div class="wrap flex-wrap flex-vertical">
      <header id="header">
        <div class="header-wrap">
          <div @click="goback" class="header-left">
            <i class="iconfont icon-pre"></i>
          </div>
          <div class="header-title">绑定手机号码</div>
        </div>
      </header>
      <div id="main" class="flex-con">
        <div class="form-wrap">
          <div class="form-item flex-wrap">
            <i class="iconfont icon-shouji"></i>
            <input v-model="mobile" class="flex-con" type="tel" placeholder="请输入手机号码">
          </div>
          <div class="form-item flex-wrap">
            <i class="iconfont icon-yanzhengma"></i>
            <input v-model="code" class="flex-con" type="text" placeholder="请输入验证码">
            <button :disabled="disabled" @click="sendSMS" :class="['sendMessage-btn', disabled ? 'disabled' : '']" type="button">{{verifyCodeBtnText}}</button>
          </div>
          <div class="btn-wrap">
            <button @click="openmain" type="button" :disabled="isDisabled" :class="[!isDisabled ? 'btn-active' : 'btn']">确定</button>
          </div>
        </div>
        <div class="agreement-wrap">
          <div @click="pass">
            <span>跳过</span>
          </div>
        </div>
      </div>
    </div>
  </div>
</body>

</html>
<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript" src="../script/vue.js"></script>
<script type="text/javascript" src="../script/common.js"></script>
<script type="text/javascript">
  var app;
  apiready = function () {
    $api.fixStatusBar($api.dom('#header'));
    api.setStatusBarStyle({
      style: 'light',
    });
    app = new Vue({
      el: '#app',
      data: {
        mobile: '',
        password: '',
        code: '',
        disabled: false,
        verifyCodeBtnText: '获取验证码',
        isDisabled: true,
      },
      created: function () {},
      mounted: function () {},
      methods: {
        watchModel: function () {
          if (this.mobile == '' || this.code == '') {
            this.isDisabled = true;
          } else {
            this.isDisabled = false;
          }
        },

        goback: function () {
          api.closeWin();
        },

        ctimer: function (time) {
          var t = time;
          var that = this;
          if (t > 0) {
            that.disabled = true;
            that.verifyCodeBtnText = t + '秒';
            t--;
            setTimeout(function () {
              that.ctimer(t);
            }, 1000);
          } else {
            that.disabled = false;
            that.verifyCodeBtnText = '获取验证码';
          }
        },

        sendSMS: function () {
          var _this = this;
          if (_this.mobile == '') {
            api.toast({
              msg: '手机号码不能为空',
              duration: 2000,
              location: 'bottom'
            });
            return false;
          }
          api.ajax({
            url: baseUrl + 'oauth/regsms',
            method: 'post',
            headers: {
              token: $api.getStorage('deviceToken')
            },
            data: {
              values: {
                mobile: _this.mobile,
              },
            }
          }, function (ret, err) {
            _this.ctimer(60);
            if (ret) {
              api.toast({
                msg: ret.message[0]
              });
            }
            if (err) {
              api.toast({
                msg: '网络错误，请稍后再试'
              });
            }
          });
        },

        openmain: function () {
          var _this = this;
          if (_this.mobile == '') {
            api.toast({
              msg: '手机号码不能为空',
              duration: 2000,
              location: 'bottom'
            });
            return false;
          }
          if (_this.code == '') {
            api.toast({
              msg: '验证码不能为空',
              duration: 2000,
              location: 'bottom'
            });
            return false;
          }
          api.showProgress({
            title: '',
            text: '',
          });
          api.ajax({
            url: baseUrl + 'oauth/wechat/reg',
            method: 'post',
            headers: {
              token: $api.getStorage('deviceToken')
            },
            data: {
              values: {
                uid: $api.getStorage('uid'),
                mobile: _this.mobile,
                code: _this.code,
              },
            }
          }, function (ret, err) {
            api.hideProgress();
            if (ret) {
              api.toast({
                msg: ret.message[0]
              });
              if (ret.status == 200) {
                $api.setStorage('umobile',_this.mobile);
                setTimeout(function () {
                  api.openWin({
                    name: 'main',
                    url: 'main.html',
                    slidBackEnabled: false
                  });
                }, 1000);
              }
            }
            if (err) {
              api.toast({
                msg: '网络错误，请稍后再试'
              });
            }
          });
        },
        pass: function () {
          api.openWin({
            name: 'main',
            url: 'main.html',
            slidBackEnabled: false
          });
        }
      },
      watch: {
        'mobile': function () {
          this.watchModel()
        },
        'code': function () {
          this.watchModel()
        },
      }
    })
  }
</script>